<?php


class Sherpa_Hook_TwoForOneCampaign
{

    public function viewCartIndexBasketSubtotal(WSB_Xtension_View $view)
    {
        $price = $this->calcPrice($view->products);
        if(!empty($price)) {
            return '
    			<tr>
    				<td class="label">
    					2f&uuml;r1-Aktion
    				</td>
    				<td class="price">
    					<span class="currency">CHF</span> &ndash;' . $price . '
    				</td>
    			</tr>
            ';
        }
    }

    public function viewCartIndexProductsSubtotal(WSB_Xtension_View $view)
    {
        $price = $this->calcPrice($view->products);
        if(!empty($price)) {
    		return '<tr class="bsk-details-total">
    			<td colspan="3" class="label">
    				2f&uuml;r1-Aktion
    			</td>
    			<td class="price">
    				<span class="currency">CHF</span> &ndash;' . $price . '

    			</td>
    		</tr>';
        }
    }

    public function viewBasketLastAddedSubtotal(WSB_Xtension_View $view)
    {
        $price = $this->calcPrice($view->products);
        if(!empty($price)) {
    		return '<tr class="subtotal">
    			<td>2f&uuml;r1-Aktion</td>
    			<td style="text-align:right"><span class="currency">CHF</span> &ndash;' . $price . '</td>
    		</tr>';
        }
    }

    public function modelBasketProductsCalculateTotal(&$total, Sherpa_Model_BasketProducts &$rows)
    {
        $price = $this->calcPrice($rows);
        if(!empty($price)) {
            $total = $total - $price;
        }
    }

    public function viewCheckoutSummarySubtotal(WSB_Xtension_View $view)
    {
        $price = $this->calcPrice($view->products);
        if(!empty($price)) {
    		return '<tr class="bsk-summary2-total">
    			<td colspan="3" class="label">
    				2f&uuml;r1-Aktion
    			</td>
    			<td class="price">
    				<span class="currency">CHF</span> &ndash;' . $price . '
    			</td>
    		</tr>';
        }
    }

    public function viewCheckoutConfirmationSubtotal(WSB_Xtension_View $view)
    {
        $price = $this->calcPrice($view->products);
        if(!empty($price)) {
    		return '<tr class="bsk-confirmation-total">
    			<td colspan="3" class="label">
    				2f&uuml;r1-Aktion
    			</td>
    			<td class="price">
    				<span class="currency">CHF</span> &ndash;' . $price . '
    			</td>
    		</tr>';
        }
    }

    public function viewCheckoutMailSubtotal(WSB_Xtension_View $view)
    {
        $price = $this->calcPrice($view->products);
        if(!empty($price)) {
            $str = '- 2f&uuml;r1-Aktion: CHF ' . $price . PHP_EOL;
            return utf8_encode(html_entity_decode($str));
        }
    }

    public function checkoutControllerOrderActionSubtotal(&$subtotal, &$products)
    {
        $price = $this->calcPrice($products);
        if(!empty($price)) {
            $subtotal = $subtotal - $price;
        }
    }

    private function calcPrice(Sherpa_Model_BasketProducts $basketProducts)
    {
        //$parentProductIds = array(176, 182);
        $articleNumbers = array('VFW09002', 'VFM09003', 'JFM09004', 'JFW09003');
        $products = array();
        foreach($basketProducts AS $product) {
            //if(in_array($product->parentProductId, $parentProductIds)) {
            if(in_array($product->articleNumber, $articleNumbers)) {
                // Zusammengefasste Produkte aufloesen in einen Eintrag
                // mit Anzahl 1 pro Produkt
                for($i=0; $i<$product->quantity; $i++) {
                    $products[] = array(
                        'parentProductId' => $product->parentProductId,
                        'productId' => $product->productId,
                        'price' => $product->price
                    );
                }
            }
        }

        // Mindestens zwei Produkte muessen im Warenkorb sein
        if(count($products) > 1) {
            usort($products, array($this, 'compare'));

            $i = 0;
            $pairPrices = array();
            foreach($products AS $index=>$p) {
                if($index % 2 == 0) {
                    $i++;
                }
                $pairPrices[$i][] = $p['price'];
            }

            $discount = 0;
            foreach($pairPrices AS $pairPrice) {
                if(!empty($pairPrice[1])) {
                    $discount = $discount + $pairPrice[1];
                }
            }

            return number_format($discount, 2, '.', "'");
        }
        return 0;
    }

    private function compare($a, $b)
    {
        if($a['price'] == $b['price']) {
            return 0;
        }
        return ($a['price'] > $b['price']) ? -1 : 1;
    }

}
